package com.etcsoft.examples.snippet;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class Snippet016TableLayout {

    private class MyLabelProvider extends LabelProvider implements
            ITableLabelProvider {

        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }

        @Override
        public String getColumnText(Object element, int columnIndex) {
            return columnIndex + " - " + element;
        }

    }

    public class MyModel {
        public int counter;

        public MyModel(int counter) {
            this.counter = counter;
        }

        @Override
        public String toString() {
            return "Item " + this.counter;
        }
    }

    public Snippet016TableLayout(Composite comp) {
        final TableViewer v = new TableViewer(new Table(comp, SWT.BORDER));
        v.setLabelProvider(new MyLabelProvider());
        v.setContentProvider(ArrayContentProvider.getInstance());
        v.getTable().setHeaderVisible(true);

        TableColumnLayout ad = new TableColumnLayout();
        comp.setLayout(ad);

        TableColumn column1 = this.createTableColumn(v.getTable(), "Column 1");
        TableColumn column2 = this.createTableColumn(v.getTable(), "Column 2");

        ad.setColumnData(column1, new ColumnWeightData(90, 290));
        ad.setColumnData(column2, new ColumnWeightData(10, 200));

        MyModel[] model = this.createModel();
        v.setInput(model);
        v.getTable().setLinesVisible(true);
    }

    private TableColumn createTableColumn(Table table, String textColumn) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(textColumn);
        column.setMoveable(true);
        return column;
    }

    private MyModel[] createModel() {
        MyModel[] elements = new MyModel[10];

        for (int i = 0; i < 10; i++) {
            elements[i] = new MyModel(i);
        }

        return elements;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        // shell.setSize(400, 150);
        shell.setLayout(new FillLayout());

        new Snippet016TableLayout(shell);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();

    }

}